﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Brain
{
    public class DefaultAnimationManager : IAnimationManager
    {
        /// <summary>
        /// How much frames this Animation should advance in 1 second
        /// (default is 60). Even if the game is running at 900 FPS, the Animation
        /// will run the same as it was on 60 FPS(not tested with 30, but might work as well)
        /// </summary>
        public virtual int FramesPerSecond
        {
            get { return framesPerSecond; }
            set
            {
                framesPerSecond = value;
                millisecondsPerFrame = ((1 / (float)FramesPerSecond) * 1000);
            }
        }
        private int framesPerSecond;
        private float millisecondsPerFrame;

        public bool ShouldUpdate { get; set; }

        public bool Playing { get; set; }

        private int currentFrame;
        public int CurrentFrame
        {
            get { return currentFrame; }
            set
            {
                currentFrame = value;
                SetCurrentFrame(value);
            }
        }
        public float CurrentFloatFrame { get; protected set; }

        public float FrameDifference { get; protected set; }

        public virtual void Play()
        {
            ShouldUpdate = true;
            Playing = true;
        }
        public virtual void Pause()
        {
            ShouldUpdate = true;
            Playing = false;
        }
        public virtual void Stop()
        {
            Playing = false;
            ShouldUpdate = true;
            CurrentFrame = 0;
            FrameDifference = 0;
        }

        public DefaultAnimationManager()
        {
            FramesPerSecond = 60;

            ShouldUpdate = false;
        }

        public virtual void Dispose()
        {
            this.FramesPerSecond = 0;
            Stop();
        }

        public virtual void Reset()
        {
            this.FramesPerSecond = 60;
            Stop();
        }

        public virtual void SetCurrentFrame(float frame)
        {
            FrameDifference = (float)Engine.Instance.GameTime.ElapsedGameTime.TotalMilliseconds / millisecondsPerFrame;
            currentFrame = (int)frame;
            CurrentFloatFrame = frame;
        }

        public virtual void Update()
        {
            if (Playing)
            {
                if (Engine.Instance.GameTime.ElapsedGameTime.Milliseconds != 0)
                {
                    // 60 frames in a second = 1 frame each 16 milliseconds
                    FrameDifference = (float)Engine.Instance.GameTime.ElapsedGameTime.TotalMilliseconds / millisecondsPerFrame;

                    CurrentFloatFrame += FrameDifference;
                    currentFrame = (int)CurrentFloatFrame;
                }
            }
        }

        public SerializationData Data { get; set; }

        public void SerializeData(DefaultSerializationManager manager)
        {
        }

        public void DeserializeData(DefaultSerializationManager manager, SerializationData newData)
        {
        }


    }
}
